﻿using System.Collections.Generic;
using System.Linq;

namespace MedReg
{
    public partial class AddressEntity
    {
        public void ImportList(List<Classes.AddressEntity> AddressEntityList)
        {
            var mdb = new MedregDBDataContext();

            var sTer = new Classes.Territory();
            var sKl = new Classes.KLADR();
            var sRegType = new Classes.RegistrationType();

            var Ter = new Territory();
            Ter.ImportList(sTer.getTerritoryList(AddressEntityList));
            var Kl = new KLADR();
            Kl.ImportList(sKl.getKLADRCityList(AddressEntityList));
            Kl.ImportList(sKl.getKLADRDistrictList(AddressEntityList));
            var RegType = new RegistrationType();
            RegType.ImportList(sRegType.getRegistrationTypeList(AddressEntityList));

            foreach (Classes.AddressEntity addressEntity in AddressEntityList)
            {
                var addressEntry1 = new AddressEntity
                                        {
                                            Region = addressEntity.Region.ID,
                                            District = addressEntity.District.ID,
                                            City = addressEntity.City.ID,
                                            Street = addressEntity.Street,
                                            House = addressEntity.House,
                                            Building = addressEntity.Building,
                                            Apartment = addressEntity.Apartment,
                                            Registration = addressEntity.Registration.ID,
                                            RegistrationDate = addressEntity.RegistrationDate,
                                            Employee = addressEntity.Employee.ID
                                        };

                IEnumerable<AddressEntity> query = (from cr in mdb.AddressEntity
                                                    where cr.Employee == addressEntity.Employee.ID
                                                    select cr
                                                   ).ToList();
                mdb.AddressEntity.DeleteAllOnSubmit(query);
                mdb.AddressEntity.InsertOnSubmit(addressEntry1);
            }
            mdb.SubmitChanges();
        }
    }
}